﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WgFrame.Utils;
using WgFrame.Net;

public class Equip
{
    /// <summary>
    /// 唯一id
    /// </summary>
    public long id { get; set; }

    /// <summary>
    /// 上阵位置 0.没有上阵 1-6
    /// </summary>
    public byte szPos { get; set; }

    /// <summary>
    /// 静态表id
    /// </summary>
    public int TableId { get; set; }

    /// <summary>
    /// 强化等级
    /// </summary>
    public short QiangHuaLevel { get; set; }

    /// <summary>
    /// 精炼等级
    /// </summary>
    public short JingLianLevel { get; set; }

    /// <summary>
    /// 灵石等级
    /// </summary>
    public short[] LingShiLevel { get; set; }

    /// <summary>
    /// 注灵等级
    /// </summary>
    public short ZhuLingLevel { get; set; }

    /// <summary>
    /// 属性
    /// </summary>
    public Attribute Attribute { get; private set; }

    /// <summary>
    /// 静态数据
    /// </summary>
    public InitEquip InitEquip { get; private set; }

    public Equip()
    {
        LingShiLevel = new short[4];
        Attribute = new Attribute();
    }

    public Equip(Hashtable info):this()
    {
        id = (long)info[(byte)1];
        TableId = (int)info[(byte)2];
        QiangHuaLevel = (short)info[(byte)3];
        JingLianLevel = (short)info[(byte)4];
        LingShiLevel = (short[])info[(byte)5];
        ZhuLingLevel = (short)info[(byte)6];
        szPos = (byte)info[(byte)7];

        if (InitEquipMgr.Dict.TryGetValue(TableId, out InitEquip initEquip))
        {
            InitEquip = initEquip;
        }
        CountAttribute();
    }

    public int GetTotalLingShiLevel()
    {
        int s = 0;
        for (int i=0; i<LingShiLevel.Length; i++)
        {
            s += LingShiLevel[i];
        }
        return s;
    }

    public void CountAttribute()
    {
        if (InitEquip == null)
        {
            return;
        }
        Attribute.Clear();

        int value = InitEquip.value;

        if (EquipQiangHuaMgr.Dict.TryGetValue(QiangHuaLevel, out EquipQiangHua qh))
        {
            value += qh.GetValue(InitEquip.kind);
        }

        if (EquipJianLianMgr.Dict.TryGetValue(JingLianLevel, out EquipJianLian jl))
        {
            value += jl.GetValue(InitEquip.kind);
        }

        Attribute.Add(InitEquip.GetAttributeType(), value);

        if (InitEquip.kind == 1)
        {
            if (ZhuLingWeaponMgr.Dict.TryGetValue(ZhuLingLevel, out ZhuLingWeapon zl))
            {
                Attribute.Add(AttributeType.ATK, zl.atk);
                Attribute.Add(AttributeType.KBL, zl.kbl);
            }
        } 
        else if (InitEquip.kind == 2)
        {
            if (ZhuLingClothesMgr.Dict.TryGetValue(ZhuLingLevel, out ZhuLingClothes zl))
            {
                Attribute.Add(AttributeType.DEF, zl.def);
                Attribute.Add(AttributeType.EVD, zl.evd);
            }
        }
        else if (InitEquip.kind == 3)
        {
            if (ZhuLingRingMgr.Dict.TryGetValue(ZhuLingLevel, out ZhuLingRing zl))
            {
                Attribute.Add(AttributeType.HP, zl.hp);
                Attribute.Add(AttributeType.DEX, zl.dex);
            }
        }
        else if (InitEquip.kind == 4)
        {
            if (ZhuLingShoeMgr.Dict.TryGetValue(ZhuLingLevel, out ZhuLingShoe zl))
            {
                Attribute.Add(AttributeType.RGS, zl.rgs);
                Attribute.Add(AttributeType.CRT, zl.crt);
            }
        }

        for (int i=0; i<LingShiLevel.Length; i++)
        {
            if (EquipLingShiMgr.Dict.TryGetValue(LingShiLevel[i], out EquipLingShi ls))
            {
                if (i == 0)
                {
                    if (ls.atk > 0)
                    {
                        Attribute.Add(AttributeType.ATK, ls.atk);
                    }
                }
                else if (i == 1)
                {
                    if (ls.def > 0)
                    {
                        Attribute.Add(AttributeType.DEF, ls.def);
                    }
                }
                else if (i == 2)
                {

                    if (ls.rgs > 0)
                    {
                        Attribute.Add(AttributeType.RGS, ls.rgs);
                    }
                }
                else if (i == 3)
                {
                    if (ls.hp > 0)
                    {
                        Attribute.Add(AttributeType.HP, ls.hp);
                    }
                }
            }
        }
    }
}
